package cz.gelion.shylock.invoice.entity

import scala.beans.BeanProperty
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Table
import java.util.Calendar
import javax.persistence.Temporal
import java.util.Set
import javax.persistence.OneToMany
import javax.persistence.ManyToOne
import javax.persistence.JoinColumn
import javax.persistence.TemporalType
import javax.persistence.Id
import javax.persistence.GeneratedValue
import java.util.GregorianCalendar
import javax.persistence.PreUpdate
import javax.persistence.PrePersist
import scala.util.DynamicVariable
import cz.gelion.shylock.invoice.CurrentEnv
import cz.gelion.shylock.invoice.ManagedInfo
import cz.gelion.shylock.invoice.Log
import javax.persistence.AccessType
import javax.persistence.Access

@Entity
@Table(name="subject")
class Subject extends ManagedInfo {
  
  @Id
  @GeneratedValue
  @BeanProperty
  @Column(name="id")
  var id:Int = _
  
  @BeanProperty
  @Column(name="prefix")
  var prefix:String = _
  
  @BeanProperty
  @Column(name="name")
  var name:String = _
  
  @BeanProperty
  @Column(name="givenname")
  var givenname:String = _
  
  @BeanProperty
  @Column(name="suffix")
  var suffix:String = _
  
  @BeanProperty
  @Column(name="street")
  var street:String = _
  
  @BeanProperty
  @Column(name="city")
  var city:String = _
  
  @BeanProperty
  @Column(name="district")
  var district:String = _
  
  @BeanProperty
  @Column(name="state")
  var state:String = _
  
  @BeanProperty
  @Column(name="postcode")
  var postcode:String = _
  
  @BeanProperty
  @Column(name="taxno")
  var taxno:String = _
  
  @BeanProperty
  @Column(name="idno")
  var idno:String = _;
  
  @BeanProperty
  @Column(name="note")
  var note:String = _
  
  @BeanProperty
  @Column(name="mtime")
  @Temporal(TemporalType.TIMESTAMP)
  var mtime:Calendar = _
  
  @BeanProperty
  @OneToMany(mappedBy="subject")
  var units:Set[SubjectUnit] = _
  
  @BeanProperty
  @OneToMany(mappedBy="subject")
  var accounts:Set[Account] = _
  
  
  @BeanProperty
  @ManyToOne
  @JoinColumn(name="muser")
  var manager:Manager = _
  
  
  @PreUpdate
  @PrePersist
  def preUpdate = {
    stamp
  }
 
  
  def label:String = {
    val f = (p:String,v:String) => if (v != null) p + v else ""
    name + f(" ", suffix) + f(",", givenname) + f(", ",  prefix) 
  }

  
  
}
